﻿
using System ;

using MskEdit.Dxf.Representation ;
using MskEdit.Dxf.Model ;


namespace MskEdit.Dxf.Backend
{

    public static class DxfLineReader
    {
        internal static bool CanRead( string[] allLines, int lineNo ) {
        
            if ( allLines.Length <= lineNo )
                return false ;
                
            return true ;                
        }


        internal static bool CanRead( string codeStr, string[] allLines, int lineNo ) {
        
            if ( allLines.Length <= ( lineNo + 1 ) )
                return false ;
                
            if ( ! allLines[lineNo].Equals( codeStr ) )
                return false ;
                
            return true ;                
        }


        internal static bool CanRead( string codeStr, string target, string[] allLines, int lineNo ) {
                        
            if ( ! CanRead( codeStr, allLines, lineNo ) )
                return false ;
                
            if ( ! allLines[lineNo + 1].Equals( target ) )
                return false ;
                
            return true ;                
        }
        
        
        internal static string TryPeek( string[] allLines, int lineNo, string fallbackString ) {
        
            if ( ! CanRead( allLines, lineNo ) )
                return fallbackString ;
            
            return allLines[lineNo] ;
        }
        
        
        internal static string TryRead( string[] allLines, ref int lineNo, string fallbackString ) {
        
            if ( ! CanRead( allLines, lineNo ) )
                return fallbackString ;
                                
            lineNo += 1 ;
            
            return allLines[lineNo - 1] ;
        }
        
        
        internal static string TryRead( string codeStr, string[] allLines, ref int lineNo, string fallbackString ) {
        
            if ( ! CanRead( codeStr, allLines, lineNo ) )
                return fallbackString ;
                                
            lineNo += 2 ;
            
            return allLines[lineNo - 1] ;
        }


        internal static string TryRead( string codeStr, string target, string[] allLines, ref int lineNo, string fallbackString ) {
        
            if ( ! CanRead( codeStr, target, allLines, lineNo ) )
                return fallbackString ;
                                
            lineNo += 2 ;
            
            return allLines[lineNo - 1] ;
        }

    }
}
